<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					icon="el-icon-finished"
					type="primary"
					:disabled="selection.length == 0"
					@click="pro_status"
					>已处理</el-button
				>
				<el-button
					icon="el-icon-delete"
					plain
					type="danger"
					:disabled="selection.length == 0"
					@click="batch_del"
				/>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input
						v-model="param.key"
						clearable
						placeholder="关键字"
					/>
					<el-button
						icon="el-icon-search"
						type="primary"
						@click="search"
					/>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:api-obj="apiObj"
				:column="column"
				row-key="id"
				:menu-default="['delete']"
				@menu-handle="menuHandle"
				@selection-change="selectionChange"
			>
				<!-- 固定列-选择列 -->
				<el-table-column fixed type="selection" width="60" />
				<el-table-column
					label="#"
					type="index"
					width="50"
				></el-table-column>
				<template #status="{ data }">
					<el-tag
						disable-transitions
						:type="data.status ? 'success' : 'danger'"
					>
						{{ data.status ? "已发放" : "未发放" }}
					</el-tag>
				</template>
				<template #userId="{ data }">
					<el-avatar
						class="user-avatar"
						:src="resHead(data.user)"
					/>{{ data.user.nickName }}
				</template>
				<template #prizeId="{ data }">
					{{ data.prize.name }}
				</template>
			</scTable>
		</el-main>
	</el-container>
</template>
<script>
export default {
	components: {},
	data() {
		return {
			apiObj: this.$API.carsprizelog.page,
			list: [],
			param: {
				key: "",
			},
			selection: [],
			column: [
				{
					label: "id",
					prop: "id",
					width: "200",
					sortable: true,
					hide: true,
				},
				{
					prop: "userId",
					label: "用户编号",
					width: 160,
					align: "left",
				},
				{ prop: "prizeId", label: "奖品ID", width: 200, align: "left" },
				{ prop: "prizeInfo", label: "中奖信息" },
				{ prop: "status", label: "发放状态", width: 100 },
				{ prop: "createTime", label: "中奖时间", width: 180 },
			],
		};
	},
	mounted() {},
	methods: {
		complete() {
			this.$refs.table.refresh();
		},
		search() {
			this.$refs.table.upData(this.param);
		},
		pro_status() {
			this.$confirm(
				`确定处理选中的 ${this.selection.length} 项已读状态吗？`,
				"提示",
				{
					type: "warning",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}
			)
				.then(async () => {
					const loading = this.$loading();
					let ids = [];
					this.selection.forEach((element) => {
						ids.push(element.id);
					});
					var res = await this.$API.carsprizelog.status.put(ids);
					if (res.code == 200) {
						this.$refs.table.refresh();
						loading.close();
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				})
				.catch(() => {});
		},
		//删除
		async table_del(row) {
			var res = await this.$API.carsprizelog.delete.delete(row.id);
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//批量删除
		batch_del() {
			this.$confirm(
				`确定删除选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}
			)
				.then(async () => {
					const loading = this.$loading();
					let ids = [];
					this.selection.forEach((element) => {
						ids.push(element.id);
					});
					var res = await this.$API.carsprizelog.delete.delete(
						ids.join(",")
					);
					if (res.code == 200) {
						this.$refs.table.refresh();
						loading.close();
						this.$message.success("删除成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				})
				.catch(() => {});
		},
		resHead(data) {
			if (!data.headPic && data.headPic.indexOf("http") == -1) {
				data.headPic = this.$CONFIG.SERVER_URL + data.headPic;
			}
			return data.headPic;
		},
		selectionChange(selection) {
			this.selection = selection;
		},
		menuHandle(obj){
			if(obj.command=='delete'){
				this.table_del(obj.row)
			}
		}
	},
};
</script>
<style>
.user-avatar {
	vertical-align: middle;
	margin-right: 5px;
}
</style>
